package com.chixing.controller;

import com.chixing.service.RegisterService;
import com.chixing.util.RSAUtils;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: wtl
 * @Date: 2021/10/17/3:28
 * @Description:
 */
@Controller
//@RequestMapping("xxx")
public class RegisterController {
    @Autowired
    private RegisterService registerService;
    @PostMapping("register")
    public ModelAndView getCustTelno(Long custTelno,String custPassword, HttpSession session) {
        System.out.println("前段返回的密码:" + custPassword);
        System.out.println("session id:"+session.getId());
        ModelAndView mav = new ModelAndView();
        ServerResponse serviceResponse = registerService.getCustTelno(custTelno);
        if(serviceResponse.getData().equals("existence")){
            System.out.println("存在");
            mav.addObject("loginMsg","手机号码已被注册过请返回登录");
            mav.setViewName("customer/register");
        }else{
            registerService.save(custTelno,custPassword);
            mav.setViewName("customer/login");
        }
        return mav;
    }

    @GetMapping("publickey")
    @ResponseBody
    public ServerResponse resregister() {
        System.out.println("111publickey");

        return registerService.getPublicKey();
    }

    @GetMapping("/encrypted")
    @ResponseBody
    public ServerResponse password(@RequestParam("encrypted") String encrypted) {
        System.out.println("密文: ");
        System.out.println(encrypted);

        String privateKey = registerService.getPrivateKey();

        try {
            System.out.println("解密中.....");
            String decryptedBytes = RSAUtils.privateDecrypt(encrypted, RSAUtils.getPrivateKey(privateKey));
            System.out.println("解密后：" + decryptedBytes);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
